<!--
 * @Author: thats-all-right-ha-ha 8280209+thats-all-right-ha-ha@user.noreply.gitee.com
 * @Date: 2024-03-06 22:25:17
 * @LastEditors: thats-all-right-ha-ha 8280209+thats-all-right-ha-ha@user.noreply.gitee.com
 * @LastEditTime: 2024-03-07 18:12:02
 * @FilePath: \hrManage\src\views\employee\components\RoleList.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <el-dialog title="分配角色" :visible="showRoleList" @close="btnCancel" @open="getRoleIds">
    <el-checkbox-group v-model="list">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id ">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-button type="primary" size="mini" @click="btnOk">确定</el-button>
      <el-button size="mini" @click="btnCancel">取消</el-button>
    </el-row>
  </el-dialog>
</template>

<script>
import { getEnabled } from '@/api/role'
import { updateAssignRoles } from '@/api/employee'
export default {
  props: {
    showRoleList: {
      type: Boolean,
      default: false
    },
    // eslint-disable-next-line vue/require-default-prop
    roleIds: {
      type: Array
    },
    roleId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      roleList: [],
      list: []
    }
  },
  watch: {
    showRoleList(newValue) {
      if (newValue) {
        this.getRoleIds()
      }
    }
  },
  created() {
    this.getRoleList()
    if (this.roleId) {
      this.getRoleIds()
    }
  },
  methods: {
    async getRoleList() {
      this.roleList = await getEnabled()
    },
    btnCancel() {
      this.$emit('update:showRoleList', false)
    },
    async btnOk() {
      await updateAssignRoles({
        id: this.roleId,
        roleIds: this.list
      })
      this.$message.success('角色添加成功')
      this.btnCancel()
      this.getRoleList()
    },
    getRoleIds() {
      this.list = this.roleIds
    }
  }
}
</script>

<style>

</style>
